package xyz.naoyu.entity;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import xyz.naoyu.dao.BaseDao;
import xyz.naoyu.settings;

import java.sql.SQLException;


/**
 * @author yWieLoc
 * @date 2022/3/14 14:53
 */
public class EntityBase {
    /**
     * 协助信息管理，
     * 物的基本管理信息，包括物的模和物,
     * 提供方法，根据id的信息多层次补全。
     */
    @JSONField(name = "Id")
    protected String Id;

    @JSONField(name = "type")
    protected String type;

    @JSONField(name = "creatorId")
    protected String creatorId;
    protected String createTime;

//    @JSONField(name = "status")

    @JSONField(name = "state")
    protected String state;

    @JSONField(name = "shorts")
    protected String shorts;

    // fastjson 依赖无参构造进行反序列化
    public EntityBase() {
    }

    public EntityBase(String id) {
        Id = id;
    }

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCreatorId() {
        return creatorId;
    }

    public void setCreatorId(String creatorId) {
        this.creatorId = creatorId;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getShorts() {
        return shorts;
    }

    public void setShorts(String shorts) {
        this.shorts = shorts;
    }


    /**
     * 从数据库获得详细信息，依赖rowkey
     * @param tools     数据库连接工具
     * @return  从数据库获得的该对象数据
     * @throws Exception
     */
    public Object getInfo(BaseDao tools) {
        if(Id == null || Id.equals("")) {
            return null;
        } else {
            JSONArray res = JSON.parseArray( tools.getData(type,Id) );
//            return res.getJSONObject(0).toJavaObject(this.getClass());
            if(res.isEmpty()) return this;
            return res.getJSONObject(0).toJavaObject(this.getClass());
        }
    }



    /**
     * 抹除细节信息
     * @param
     * @throws
     */
    public void InfoLimit() {
        if(state == null || state == settings.state.getString("LOCK"))
            return;
        creatorId  = state = createTime = null;  // 限制信息清除
    }


    /**
     * 将当前信息保存到数据库，依赖Id
     * @param tools 数据库连接工具
     * @throws Exception
     */
    public int infoUpdate(BaseDao tools) throws Exception{

        if (state!= null && state.equals(settings.state.getString("LOCK"))) {
            throw new Exception("已锁定");
        }

        return tools.putData(type, JSONObject.parseObject(JSON.toJSONString(this)) );
    }


    public boolean delSelf(BaseDao tools) {
        if(Id == null)  return  false;
        return tools.delData(type,Id);
    }
}
